package com.culture.config.component;

import javax.annotation.Resource;

import com.culture.config.common.model.BusinessException;
import com.culture.config.common.util.ExceptionUtil;
import com.culture.config.dao.entity.App;
import com.culture.config.dao.mapper.AppMapper;
import com.culture.config.dao.mapper.PropertyCopyMapper;
import com.culture.config.model.po.ApplicationCopyPO;
import com.culture.config.wrapper.ApplicationWrapper;
import com.culture.config.wrapper.PropertyCopyWrapper;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AppWorker {
    @Resource
	private AppMapper appMapper;   
    @Resource
	private PropertyCopyMapper propertyCopyMapper;

    public App get(Long appId){
        App app = this.appMapper.get(appId);
        ExceptionUtil.businessExceptionWhenNull(app, "App not found.");
        return app;
    }

    public App copyApp(ApplicationCopyPO copyPO) {
		App newApp = ApplicationWrapper.wrapAppCopy(copyPO);
		int ret = this.appMapper.add(newApp);
		log.info("copyApp ret:{},appid:{}",ret,newApp.getId());
		if(ret < 1){
			log.error("copyApp failed with:{}", copyPO);
			throw new BusinessException("copyApp failed");
		}
		ret = this.propertyCopyMapper.copyByApp(PropertyCopyWrapper.wrapAppCopy(copyPO, newApp));
		log.info("copyAppProperty ret:{}, from appid:{} to {}",ret,copyPO.getCopyId(),newApp.getId());		
		return newApp;
	}
    
}
